from ib.ext.EWrapper import EWrapper
import config as cfg

#---------------------------------------------------
#  EWrapper pure virtual handlers
#---------------------------------------------------

def tickSize(self, tickerId, field, size):
    if(cfg.debug): print 'sizeMsg\n'

def tickString(self, tickerId, tickType, value):
    if(cfg.debug): print 'tickString: ' + str(value)

def tickOptionComputation(self, tickerId, field, impliedVolatility, delta, modelprice, div):
    print 'tickerId:%d field:%d ivol:%d delta:%d tv:%.2f' % (
        tickerId, field, impliedVolatility, delta, modelprice)
    

def tickSnapshotEnd(self, tickerId):
    """ not working for now"""
    print "FINITO"

def nextValidId(self, orderId):
    if( cfg.debug): print orderId

def error(self, id=None, errorCode=None, errorMsg=None):
    if( cfg.debug):  print errorMsg

def tickGeneric(self, tickerId, tickType, value):
    """ ETickType.HALTED = 49 """
    if( cfg.debug):  print 'type: %d, val: %.2f '%(tickType,value)
    
        

virtuals = dict(tickSize = tickSize, tickString = tickString,
                tickSnapshotEnd = tickSnapshotEnd,
                nextValidId = nextValidId, error = error,
                tickGeneric = tickGeneric,
                tickOptionComputation = tickOptionComputation)


class WrapperType(type):
   """ Metaclass for EWrapper generation"""
   def __init__(cls, name, bases, dct):
       cb = dict( virtuals.items() + dct.items())
       [setattr(cls,cb[h.__name__].__name__,h) for h in cb.values()]
#       cls.nextValidId = virtuals['nextValidId']
       super(WrapperType,cls).__init__(name,bases,cb)

       
#Wrapper = WrapperType('Wrapper',(EWrapper,),cfg.callbacklist)

